# -*- coding: utf-8 -*-
from scrapy import Request,Spider
from pyquery import PyQuery as pq

from skygb.items import SkygbItem


class SkydbSpider(Spider):
    name = "skydb"
    allowed_domains = ["fz.people.com.cn"]
    start_urls = ['http://fz.people.com.cn/']
    project_url = 'http://fz.people.com.cn/skygb/sk/index.php/Index/seach'

    def start_requests(self):
        yield Request(url=self.project_url,callback=self.item_parse)

    def item_parse(self, response):
        result = pq(response.text)
        project_info_list = result('.jc_a table tr')
        for project_info in project_info_list.items():
            # 去除首行
            if project_info('span').eq(0).html() == None:
                continue

            item = self.create_item(project_info('span'))
            yield item

        # 一页内容爬取完成，跳转到下一页
        init_text = result('.page a').eq(0).text()
        next_text = result('.page a').eq(1).text()
        if next_text == '下一页':
            next_url = result('.page a').eq(1).attr('href')
            yield Request(response.urljoin(url=next_url),callback=self.item_parse)
        elif init_text == '下一页':
            # 第一页
            next_url = result('.page a').eq(0).attr('href')
            yield Request(response.urljoin(url=next_url), callback=self.item_parse)



    def create_item(self,project_info):
        '''
        初始化一个社科基金信息
        :param project_info:包含社科基金信息的tr
        :return: 社科基金信息item
        '''
        item = SkygbItem()
        item['zmpzh'] = self.none_handle(project_info('span').eq(0).html())
        item['xmlb'] = self.none_handle(project_info('span').eq(1).html())
        item['xkfl'] = self.none_handle(project_info('span').eq(2).html())
        item['xmmc'] = self.none_handle(project_info('span').eq(3).html())
        item['lxsj'] = self.none_handle(project_info('span').eq(4).html())
        item['xmfzr'] = self.none_handle(project_info('span').eq(5).html())
        item['zyzw'] = self.none_handle(project_info('span').eq(6).html())
        item['gzdw'] = self.none_handle(project_info('span').eq(7).html())
        item['dwlb'] = self.none_handle(project_info('span').eq(8).html())
        item['szsqs'] = self.none_handle(project_info('span').eq(9).html())
        item['ssxt'] = self.none_handle(project_info('span').eq(10).html())
        item['cgmc'] = self.none_handle(project_info('span').eq(11).html())
        item['cgxs'] = self.none_handle(project_info('span').eq(12).html())
        item['cgdj'] = self.none_handle(project_info('span').eq(13).html())

        return item

    def none_handle(self,html):
        '''
        将none清除，转换为空字符串
        :param html:
        :return:
        '''
        if html == None:
            return ""
        else:
            return html